//
//  ViewController.m
//  测试
//
//  Created by Yasin on 2018/10/12.
//  Copyright © 2018年 ZhiYuan Network. All rights reserved.
//

#import "ViewController.h"
#include <CommonCrypto/CommonDigest.h>
#include <CommonCrypto/CommonHMAC.h>

#import "NSData+hex.h"
#import "NSString+Extension.h"
#import "FDHttpTool.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (nonatomic, strong) NSString *timeStampStr;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark - 注册
- (IBAction)registerBtn:(UIButton *)sender {
    
    NSString *phoneStr = @"13073636130";
    NSString *pwd = @"123456";
    NSDictionary *param = @{@"name":phoneStr,@"password":pwd,@"againPassword":pwd};
    
//    http://10.10.10.10:8083
    
    //登陆url
//    NSString *loginUrl = @"http://catering.api.dev.51zhiyuan.net/auth/register";
    NSString *loginUrl = @"http://10.10.10.10:8083/auth/register";

    NSString *signaStr = [self signatureStr];
    
//    NSString *temptoken = @"049b150d9bcc44d5964e545040c553fa";
    
    //生成授权
    NSString *author = [self generateAuthorizationWithToken:@"" signature:signaStr];
    
    //时间戳
//    NSString *timeStamp = [self getNowTime];
    //请求
    [FDHttpTool rb_POSTWithURL:loginUrl authr:author timeStampStr:self.timeStampStr params:param successHandler:^(id responseObjc) {

        NSLog(@"行号:%d %@",__LINE__,responseObjc);
    } failureHandler:^(NSError *error) {
        NSLog(@"行号:%d 失败:%@",__LINE__,error);
    }];
}


//三 生成授权
- (NSString *)generateAuthorizationWithToken:(NSString *)token signature:(NSString *)sigstr {
    NSString *autho = [NSString stringWithFormat:@"%@:%@",token,sigstr];
    
    NSLog(@"行数:%d   %@",__LINE__,autho);
    
    NSString *originAutho = @"";
    
    //base 编码
    
    NSLog(@"行数:%d  %@",__LINE__,[autho rb_base64EncodingString]);
    
    return [autho rb_base64EncodingString];
}

//二 生成签名
- (NSString *)signatureStr {
    NSString *api = @"/auth/register";
    NSString *device = @"ios";
//    NSString *timeStamp = @"1539312798293";
      NSString *timeStamp = [self getNowTime];
    
    self.timeStampStr = timeStamp; //
    NSString *version = @"1.0.0";
    
    //拼接 device + timeStamp+ version
    NSString *dtvValue = [NSString stringWithFormat:@"%@\n%@\n%@",device,timeStamp,version];

    //签名
    NSString *signagure = [self HmacSha1:api data:dtvValue];
    NSLog(@"3333:%@",signagure);
    return signagure;
}


//一 时间戳
#pragma mark --- 获取当前时间戳--13位
- (NSString *)getNowTime{
    
    //获取当前时间戳
    NSDate* date = [NSDate dateWithTimeIntervalSinceNow:0];//获取当前时间0秒后的时间
    NSTimeInterval time=[date timeIntervalSince1970]*1000;// *1000 是精确到毫秒，不乘就是精确到秒
    NSString *timeSp = [NSString stringWithFormat:@"%.0f", time];
    
    return timeSp;
}



#pragma mark - hmacsha1 加密 新

#pragma mark - hmacsha1 加密 有问题
//HmacSHA1加密；
- (NSString *)HmacSha1:(NSString *)key data:(NSString *)value
{
    
    NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
    NSData *valueData = [value dataUsingEncoding:NSUTF8StringEncoding];
    
    unsigned char result[CC_SHA1_DIGEST_LENGTH];
    CCHmac(kCCHmacAlgSHA1, [keyData bytes], [keyData length], [valueData bytes], [valueData length], result);
    
    NSData *HMAC = [[NSData alloc] initWithBytes:result
                                          length:sizeof(result)];
    
    NSLog(@"%d行  %@",__LINE__,HMAC);
    NSString *str =  [HMAC convertDataToHexStr:HMAC];
    NSLog(@"%d行  %@",__LINE__,str);
    
    return str;

//    const char *cKey  = [key cStringUsingEncoding:NSUTF8StringEncoding];
//    const char *cData = [data cStringUsingEncoding:NSUTF8StringEncoding];
//    //Sha256:
//    // unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
//    //CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
//
//    //sha1
//    unsigned char cHMAC[CC_SHA1_DIGEST_LENGTH];
//    CCHmac(kCCHmacAlgSHA1, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
//
//    NSData *HMAC = [[NSData alloc] initWithBytes:cHMAC length:sizeof(cHMAC)];
//
//    NSLog(@"原始数据:%@ 长度:%ld",HMAC,HMAC.length);
//
//    //转换为NSString *
//    NSString *str =  [HMAC convertDataToHexStr:HMAC];
//    NSLog(@"转换为字符串: %@ 长度:%ld",str,str.length);
//
}



@end
